/*
 *  Copyright 2019-2025 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.account.service.Impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.account.domain.AccountAc08;
import me.zhengjie.modules.account.domain.AccountAc50;
import me.zhengjie.modules.account.domain.AccountAc50a5;
import me.zhengjie.modules.account.domain.AccountAc51;
import me.zhengjie.modules.account.domain.dto.AccountAc08QueryCriteria;
import me.zhengjie.modules.account.mapper.AccountAc08Mapper;
import me.zhengjie.modules.account.mapper.AccountCommonMapper;
import me.zhengjie.modules.account.service.AccountAc08Service;
import me.zhengjie.modules.account.service.AccountAc50Service;
import me.zhengjie.modules.account.service.AccountAc51Service;
import me.zhengjie.modules.basic.domain.BasicAb01;
import me.zhengjie.modules.basic.domain.BasicAc01;
import me.zhengjie.modules.basic.domain.BasicAc02;
import me.zhengjie.modules.basic.mapper.BasicAb01Mapper;
import me.zhengjie.modules.basic.mapper.BasicAc01Mapper;
import me.zhengjie.modules.basic.service.BasicAc01Service;
import me.zhengjie.modules.basic.service.BasicAc02Service;
import me.zhengjie.modules.logs.domain.BusinessLog;
import me.zhengjie.modules.logs.service.BusinessLogService;
import me.zhengjie.modules.sequence.domain.Sequence;
import me.zhengjie.modules.sequence.service.SequenceService;
import me.zhengjie.tool.DateUtil;
import me.zhengjie.tool.ListUtil;
import me.zhengjie.tool.NPETool;
import me.zhengjie.tool.NetUtils;
import me.zhengjie.util.ValidateUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.vo.Constant;
import me.zhengjie.vo.YHConstants;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import me.zhengjie.utils.PageResult;

/**
 * @author lzw
 * @description 服务实现
 * @date 2025-06-09
 **/
@Service
@RequiredArgsConstructor
public class AccountAc08ServiceImpl extends ServiceImpl<AccountAc08Mapper, AccountAc08> implements AccountAc08Service {

    private final AccountAc08Mapper accountAc08Mapper;
    private final AccountCommonMapper accountCommonMapper;
    private final SequenceService sequenceService;
    private final AccountAc50Service accountAc50Service;
    private final BasicAc02Service basicAc02Service;
    private final AccountAc51Service accountAc51Service;
    private final BusinessLogService businessLogService;
    private final BasicAc01Mapper basicAc01Mapper;
    private final BasicAb01Mapper basicAb01Mapper;

    @Override
    public PageResult<AccountAc08> queryAll(AccountAc08QueryCriteria criteria, Page<Object> page) {
//        checkMap.put("tyjrzhdy", "1");
//        checkMap.put("zzzhdy", "1");//终止账户也需要查询打印
//        checkMap.put("ynPc06", "1");
        //    return accountCommonMapper.queryAc08(checkMap);//实缴信息
        return PageUtil.toPage(accountAc08Mapper.findAll(criteria, page));
    }

    @Override
    public List<AccountAc08> queryAll(AccountAc08QueryCriteria criteria) {

        return accountAc08Mapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(List<AccountAc08> resources) throws IOException {

        check(resources);
        String yyyyMMddHHmmss = DateUtil.getCurrentTime("yyyyMMddHHmmss");
        String currentUsername = SecurityUtils.getCurrentUsername();

        Sequence sequence_aaz648 = new Sequence();
        sequence_aaz648.setName("aaz648");
        Long aaz648 = Long.valueOf(sequenceService.nextSequence(sequence_aaz648) + "000");

        Sequence sequence_aaz223 = new Sequence();
        sequence_aaz223.setName("aaz223");
        Long aaz223 = Long.valueOf(sequenceService.nextSequence(sequence_aaz223) + "000");
        List<String> aaa115s = new ArrayList<>();
        aaa115s.add(Constant.AAA115_21_JFBLDZTS);
        aaa115s.add(Constant.AAA115_22_JFJSDZTS);
        aaa115s.add(Constant.AAA115_34_JFJSDZBJ);
        aaa115s.add(Constant.AAA115_35_JFBLDZBJ);


        Set<String> collect = resources.stream().map(AccountAc08::getAaa115).collect(Collectors.toSet());//


        Set<String> setB = collect.stream().collect(Collectors.toSet()); // 将collect转换为set以便快速查找

        boolean hasCommonElements = aaa115s.stream().anyMatch(setB::contains); // 检查是否有共同元素

        List<Map> ac08ss=new ArrayList<>();
        if(hasCommonElements){ // 存在 21 22 34 35 aaz686取原缴费记录
            Map query = new HashMap<>();
            query.put("tyjrzhdy", "1");
            query.put("zzzhdy", "1");//终止账户也需要查询打印
            query.put("ynPc06", "1");
            query.put("aaz159", resources.get(0).getAaz159());
             ac08ss = accountCommonMapper.queryAc08(query);//实缴信息
        }

        QueryWrapper<BasicAc02> queryac02 = new QueryWrapper<>();
        queryac02.eq("aaz159",resources.get(0).getAaz159());

        List<BasicAc02> listac02 = basicAc02Service.list(queryac02);

        for (AccountAc08 ac08 : resources) {

            ac08.setAaz648(aaz648.toString());//养老保险人员实收明细ID
            ac08.setAaz223(aaz223.toString());//人员征缴明细ID

            if(aaa115s.contains(ac08.getAaa115())){//如果是这种情况 aaz686取原缴费记录的aaz686
                List<Map> collect1 = ac08ss.stream().filter(ac08s -> ac08s.get("aae003").equals(ac08.getAae003().toString()) && ac08s.get("aae202").equals("1")).collect(Collectors.toList());
                ac08.setAaz686(collect1.get(0).get("aaz686").toString());//人员缴费ID
                ac08.setAae202(BigDecimal.ZERO);//实缴月数
            }else{
                ac08.setAaz686(aaz223.toString());//人员缴费ID
                ac08.setAae202(BigDecimal.ONE);//实缴月数
            }

            ac08.setAac001(ac08.getAac001());//人员编号
            ac08.setAaz159(ac08.getAaz159());//人员参保关系ID
            ac08.setAab001(ac08.getAab001());//单位编号
            ac08.setAae140(ac08.getAae140());//险种类型
            ac08.setAae002(new BigDecimal(DateUtil.getCurrentTime("yyyyMM")));//费款所属期
            ac08.setAae003(ac08.getAae003());//对应费款所属期

            ac08.setAaa115(ac08.getAaa115());//应缴类型
            String aae793="";
            String aae794="";
            if(Constant.AAA115_10_ZCYJ.equals(ac08.getAaa115())){
                aae793=Constant.AAE793_1000_ZCYJ;
                aae794="正常应缴";
            }else if(Constant.AAA115_21_JFBLDZTS.equals(ac08.getAaa115())) {
                aae793=Constant.AAE793_2100_JFBLDZTS;
                aae794="正常应缴";
            }else if(Constant.AAA115_22_JFJSDZTS.equals(ac08.getAaa115())) {
                aae793=Constant.AAE793_2200_JFJSDZTS;
                aae794="正常应缴";
            }else if(Constant.AAA115_34_JFJSDZBJ.equals(ac08.getAaa115())) {
                aae793=Constant.AAE793_3400_JFJSDZBJ;
                aae794="正常应缴";
            }else if(Constant.AAA115_35_JFBLDZBJ.equals(ac08.getAaa115())) {
                aae793=Constant.AAE793_3500_JFBLDZBJ;
                aae794="正常应缴";
            }else{
                aae793=Constant.AAE793_1000_ZCYJ;
                aae794="正常应缴";
            }


            ac08.setAae793(aae793);//地方应缴类型
            ac08.setAae794(aae794);//地方应缴类型名称
            ac08.setAaa093(Constant.AAA093_2_DWJ);//承担费用主体
            if (Constant.AAE140_110_QYZGJBYLBX.equals(ac08.getAae140())) {

                ac08.setAac066(Constant.AAC066_101_QYZG);//参保身份
                ac08.setAac313(Constant.AAC313_10101_QYZG);//地方参保身份码值
                ac08.setAac314("企业职工");//地方参保身份名称
                ac08.setAae737(Constant.AAE737_10_CBDX);//缴费资金来源
                ac08.setAab019(Constant.AAB019_10_QY);//单位类型


            } else {
                ac08.setAac066(listac02.get(0).getAac066());//参保身份
                ac08.setAac313(listac02.get(0).getAac066()+"01");//地方参保身份码值
                ac08.setAac314("财政全额拨款");//地方参保身份名称
                ac08.setAae737(Constant.AAE737_20_CZ);//缴费资金来源
                ac08.setAab019(Constant.AAB019_30_JG);//单位类型

            }


            ac08.setAab033(Constant.AAB033_2_SWDZ);//征缴方式
            ac08.setAae745("");//以个人身份参保人员缴费档次
            ac08.setAac028("");//农民工标识
            ac08.setAac040(ac08.getAae180());//工资
            ac08.setAab121(ac08.getAae180());//单位缴费基数
            ac08.setAae180(ac08.getAae180());//人员缴费基数
            ac08.setAae795(ac08.getAae180());//记账基数
            ac08.setAae871(ac08.getAae180());//缴费基数累计增加额
            ac08.setAae828(Constant.AAE828_0_F);//利息征收标志
            ac08.setAic380(null);//利息截止年月
            ac08.setAae834(Constant.AAE834_0_F);//滞纳金征收标志
            ac08.setAae845(null);//滞纳金计算截止日期
            ac08.setAae844(null);//补缴规则
            ac08.setAab191(ac08.getAab191());//到账年月
            ac08.setAae080(ac08.getAae080());//单位实缴金额
            ac08.setAae081(ac08.getAae081());//单位实缴划入个人账户金额
            ac08.setAae082(ac08.getAae083());//个人实缴金额
            ac08.setAae083(ac08.getAae083());//个人实缴划入个人账户金额
            ac08.setAae788(BigDecimal.ZERO);//个人实缴划入个人账户金额
            ac08.setAae789(BigDecimal.ZERO);//单位实缴划入个人账户金额利息
            ac08.setAae790(BigDecimal.ZERO);//个人实缴金额利息
            ac08.setAae791(BigDecimal.ZERO);//个人实缴划入个人账户金额利息
            ac08.setAae840(BigDecimal.ZERO);//单位实缴金额利息其中本年利息
            ac08.setAae841(BigDecimal.ZERO);//单位实缴划入个人账户金额利息其中本年利息
            ac08.setAae842(BigDecimal.ZERO);//个人实缴金额利息其中本年利息
            ac08.setAae843(BigDecimal.ZERO);//个人实缴划入个人账户金额利息其中本年利息
            ac08.setAae057(BigDecimal.ZERO);//单位实缴金额滞纳金
            ac08.setAae888(BigDecimal.ZERO);//个人实缴金额滞纳金
            ac08.setAae086(ac08.getAae086());//其他来源实缴金额
            ac08.setAae741(Constant.AAE741_9_QYLSHDSJ);//数据来源

            ac08.setAaa042(ac08.getAaa042());//单位缴费比例或定额标准
            ac08.setAaa043(ac08.getAaa043());//单位缴费划入个人账户比例或定额标准
            ac08.setAaa041(ac08.getAaa045());//个人缴费比例或定额标准
            ac08.setAaa045(ac08.getAaa045());//个人缴费划入个人账户比例或定额标准
            ac08.setAaz616("");//核定批次号
            ac08.setAaz615("");//核定流水号
            ac08.setAae061("");//补退流水号
            ac08.setAae108(Constant.AAE108_1_S);//参加单位结算标志
            ac08.setAab301("530111");//转出地行政区划代码
            ac08.setAab299("530111");//转移参保地行政区划代码
            ac08.setAae729(null);//转入业务起息年月
            ac08.setAae819("");//缴费备用字段1
            ac08.setAae820(ac08.getAae820());//缴费备用字段2 (用来存放明细类型 区分是一般账户 还是年金划账)
            ac08.setAae796(null);//双低系数（历史数据）
            ac08.setAae797(null);//固化社平工资（历史数据）
            ac08.setAae798(null);//基数比例（历史数据）
            ac08.setAae799(null);//固化指数
            ac08.setAae800(null);//账目类型
            ac08.setAaz625(null);//征集流水号
            ac08.setAae792(Constant.AAE792_01_ZC);//费用标志
            ac08.setAae150(ac08.getAae003());//实收年月
            ac08.setAae066(aaz223.toString());//实收分配流水号
            ac08.setAae748(null);//退费年月
            ac08.setAae822(null);//退还账户滚存利息标志
            ac08.setAaz631(null);//退费批次号
            ac08.setAaz650(null);//退费流水号
            ac08.setAae013("");//备注
            ac08.setAae011(currentUsername);//经办人
            ac08.setAae036(new BigDecimal(yyyyMMddHHmmss));//经办时间
            ac08.setAab359("530111");//经办机构
            aaz223++;
            aaz648++;
        }



        QueryWrapper<AccountAc50> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("aaz159",resources.get(0).getAaz159());
        queryWrapper.eq("aae100",Constant.AAE100_1_YX);

        List<AccountAc50> list = accountAc50Service.list(queryWrapper);

        Set<String> aac062s = list.stream()
                .map(row -> row.getAac062()) // 提取字段值并转为 String
                .collect(Collectors.toSet()); // 自动去重

        Set<String> aae820s = resources.stream()
                .map(row -> row.getAae820()) // 提取字段值并转为 String
                .collect(Collectors.toSet()); // 自动去重

        //判断 List aae820s 中哪些元素不在 aac062s 中并新增
        List<String> collect1 = aae820s.stream().filter(element -> !aac062s.contains(element)).collect(Collectors.toList());



        if(collect1.size()>0){//不存在有效账户 新增明细的时候新增一个

            ArrayList<AccountAc50> ac50list = new ArrayList<>();
            ArrayList<AccountAc51> ac51list = new ArrayList<>();
            for (String s : collect1) {

                BigDecimal date = new BigDecimal(DateUtil.getCurrentTime("yyyyMMdd"));
                Sequence sequence_aaz116 = new Sequence();
                sequence_aaz116.setName("aaz116");
                String aaz116 = sequenceService.nextSequence(sequence_aaz116);

                Sequence sequence_aaz117 = new Sequence();
                sequence_aaz117.setName("aaz117");
                String aaz117 = sequenceService.nextSequence(sequence_aaz117);

                AccountAc50 accountAc50 = new AccountAc50();

                accountAc50.setAaz116(aaz116);
                accountAc50.setAac001(resources.get(0).getAac001());

                accountAc50.setAaa119(Constant.AAE140_110_QYZGJBYLBX.equals(resources.get(0).getAae140()) ? Constant.AAA119_11_QYZGJBYLBXGRZH : Constant.AAA119_12_JGSYDWJBYLBXGRZH);

                accountAc50.setAac062(s);//区别点:建立账户原因不一致
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
                String yyyymmStr = sdf.format(listac02.get(0).getAae206());
                accountAc50.setAae206(new BigDecimal(yyyymmStr));

                accountAc50.setAic376(date);

                accountAc50.setAae238(BigDecimal.ZERO);
                accountAc50.setAae239(BigDecimal.ZERO);
                accountAc50.setAae240(BigDecimal.ZERO);
                accountAc50.setAae368(Constant.AAE368_0_F);
                accountAc50.setAae100(Constant.AAE100_1_YX);
                accountAc50.setAaz159(resources.get(0).getAaz159());
                accountAc50.setAae013("");
                accountAc50.setAae011(SecurityUtils.getCurrentUsername());
                accountAc50.setAae036(date);
                accountAc50.setAab359("530111");


                AccountAc51 accountAc51 = new AccountAc51();
                accountAc51.setAaz117(aaz117);
                accountAc51.setAaz116(aaz116);
                accountAc51.setAaa119(Constant.AAE140_110_QYZGJBYLBX.equals(resources.get(0).getAae140()) ? Constant.AAA119_11_QYZGJBYLBXGRZH : Constant.AAA119_12_JGSYDWJBYLBXGRZH);
                accountAc51.setAic376(date);
                accountAc51.setAac001(resources.get(0).getAac001());
                accountAc51.setAae001(new BigDecimal(resources.get(0).getAae003().toString().substring(0, 4)));
                accountAc51.setAae849(BigDecimal.ZERO);
                accountAc51.setAae259(BigDecimal.ZERO);
                accountAc51.setAae262(BigDecimal.ZERO);
                accountAc51.setAae263(BigDecimal.ZERO);
                accountAc51.setAae850(BigDecimal.ZERO);
                accountAc51.setAae851(BigDecimal.ZERO);
                accountAc51.setAae260(BigDecimal.ZERO);
                accountAc51.setAae264(BigDecimal.ZERO);
                accountAc51.setAae265(BigDecimal.ZERO);
                accountAc51.setAae852(BigDecimal.ZERO);
                accountAc51.setAae853(BigDecimal.ZERO);
                accountAc51.setAae854(BigDecimal.ZERO);
                accountAc51.setAae855(BigDecimal.ZERO);
                accountAc51.setAae856(BigDecimal.ZERO);
                accountAc51.setAae857(BigDecimal.ZERO);
                accountAc51.setAae873(BigDecimal.ZERO);
                accountAc51.setAae874(BigDecimal.ZERO);
                accountAc51.setAae875(BigDecimal.ZERO);
                accountAc51.setAae876(BigDecimal.ZERO);
                accountAc51.setAaa031(BigDecimal.ZERO);
                accountAc51.setAae825(Constant.AAE825_0_F);
                accountAc51.setAae100(Constant.AAE100_1_YX);
                accountAc51.setAaz159(resources.get(0).getAaz159());
                accountAc51.setAae013("");
                accountAc51.setAae011(SecurityUtils.getCurrentUsername());
                accountAc51.setAae036(date);
                accountAc51.setAab359("530111");

                ac50list.add(accountAc50);
                ac51list.add(accountAc51);

            }
            accountAc50Service.saveBatch(ac50list);
            accountAc51Service.saveBatch(ac51list);

        }

        saveBatch(resources);

        saveLog(resources.get(0),YHConstants.AAA121_020104,yyyyMMddHHmmss);




    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AccountAc08 resources)throws IOException  {
        AccountAc08 accountAc08 = getById(resources.getAaz648());


       if(BigDecimal.ZERO.compareTo(resources.getAae180())==0){//按照定额生成明细

           resources.setAac040(resources.getAae180());//工资
           resources.setAab121(resources.getAae180());//单位缴费基数
           resources.setAae180(resources.getAae180());//人员缴费基数
           resources.setAae795(resources.getAae180());//记账基数
           resources.setAae871(resources.getAae180());//缴费基数累计增加额

           resources.setAaa041(resources.getAaa045());//个人缴费比例或定额标准

           resources.setAae080(BigDecimal.ZERO);
           resources.setAae081(BigDecimal.ZERO);
           resources.setAae083(resources.getAaa045());
           resources.setAae082(resources.getAaa045());
           resources.setAae086(BigDecimal.ZERO);


       }else{
        resources.setAac040(resources.getAae180());//工资
        resources.setAab121(resources.getAae180());//单位缴费基数
        resources.setAae180(resources.getAae180());//人员缴费基数
        resources.setAae795(resources.getAae180());//记账基数
        resources.setAae871(resources.getAae180());//缴费基数累计增加额

        resources.setAaa041(resources.getAaa045());//个人缴费比例或定额标准

        resources.setAae080(resources.getAae180().multiply(resources.getAaa042()));
        resources.setAae081(resources.getAae180().multiply(resources.getAaa043()));
        resources.setAae083(resources.getAae180().multiply(resources.getAaa045()));
        resources.setAae082(resources.getAae180().multiply(resources.getAaa045()));
        resources.setAae086(resources.getAae180().multiply(new BigDecimal(resources.getAae819())));

       }

        accountAc08.copy(resources);


        accountAc08Mapper.updateById(accountAc08);

        //写日志
        String yyyyMMddHHmmss = DateUtil.getCurrentTime("yyyyMMddHHmmss");


        saveLog(resources,YHConstants.AAA121_020105,yyyyMMddHHmmss);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<AccountAc08> ids) throws IOException {

        accountAc08Mapper.deleteBatchIds(ids);

        //写日志
        Map map= new HashMap<>();
        String yyyyMMddHHmmss = DateUtil.getCurrentTime("yyyyMMddHHmmss");

        saveLog(ids.get(0),YHConstants.AAA121_020106,yyyyMMddHHmmss);


    }

    @Override
    public void download(List<AccountAc08> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AccountAc08 accountAc08 : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("人员征缴明细ID", accountAc08.getAaz223());
            map.put("人员缴费ID", accountAc08.getAaz686());
            map.put("人员编号", accountAc08.getAac001());
            map.put("人员参保关系ID", accountAc08.getAaz159());
            map.put("单位编号", accountAc08.getAab001());
            map.put("险种类型", accountAc08.getAae140());
            map.put("费款所属期", accountAc08.getAae002());
            map.put("对应费款所属期", accountAc08.getAae003());
            map.put("应缴类型", accountAc08.getAaa115());
            map.put("地方应缴类型", accountAc08.getAae793());
            map.put("地方应缴类型名称", accountAc08.getAae794());
            map.put("承担缴费主体类型", accountAc08.getAaa093());
            map.put("参保身份", accountAc08.getAac066());
            map.put("地方参保身份码值", accountAc08.getAac313());
            map.put("地方参保身份名称", accountAc08.getAac314());
            map.put("缴费资金来源", accountAc08.getAae737());
            map.put("单位类型", accountAc08.getAab019());
            map.put("征缴方式", accountAc08.getAab033());
            map.put("以个人身份参保人员缴费档次", accountAc08.getAae745());
            map.put("农民工标识", accountAc08.getAac028());
            map.put("工资", accountAc08.getAac040());
            map.put("单位缴费基数", accountAc08.getAab121());
            map.put("人员缴费基数", accountAc08.getAae180());
            map.put("记账基数", accountAc08.getAae795());
            map.put("缴费基数累计增加额", accountAc08.getAae871());
            map.put("利息征收标志", accountAc08.getAae828());
            map.put("利息截止年月", accountAc08.getAic380());
            map.put("滞纳金征收标志", accountAc08.getAae834());
            map.put("滞纳金计算截止日期", accountAc08.getAae845());
            map.put("补缴规则", accountAc08.getAae844());
            map.put("到账日期", accountAc08.getAab191());
            map.put("单位实缴金额", accountAc08.getAae080());
            map.put("单位实缴划入个人账户金额", accountAc08.getAae081());
            map.put("个人实缴金额", accountAc08.getAae082());
            map.put("个人实缴划入个人账户金额", accountAc08.getAae083());
            map.put("单位实缴金额利息", accountAc08.getAae788());
            map.put("单位实缴划入个人账户金额利息", accountAc08.getAae789());
            map.put("个人实缴金额利息", accountAc08.getAae790());
            map.put("个人实缴划入个人账户金额利息", accountAc08.getAae791());
            map.put("单位实缴金额利息其中本年利息", accountAc08.getAae840());
            map.put("单位实缴划入个人账户金额利息其中本年利息", accountAc08.getAae841());
            map.put("个人实缴金额利息其中本年利息", accountAc08.getAae842());
            map.put("个人实缴划入个人账户金额利息其中本年利息", accountAc08.getAae843());
            map.put("单位实缴金额滞纳金", accountAc08.getAae057());
            map.put("个人实缴金额滞纳金", accountAc08.getAae888());
            map.put("其他来源实缴金额", accountAc08.getAae086());
            map.put("数据来源", accountAc08.getAae741());
            map.put("缴费月数", accountAc08.getAae202());
            map.put("单位缴费比例或定额标准", accountAc08.getAaa042());
            map.put("单位缴费划入个人账户比例或定额标准", accountAc08.getAaa043());
            map.put("个人缴费比例或定额标准", accountAc08.getAaa041());
            map.put("个人缴费划入个人账户比例或定额标准", accountAc08.getAaa045());
            map.put("核定批次号", accountAc08.getAaz616());
            map.put("核定流水号", accountAc08.getAaz615());
            map.put("补退流水号", accountAc08.getAae061());
            map.put("参加单位结算标志", accountAc08.getAae108());
            map.put("转出地行政区划代码", accountAc08.getAab301());
            map.put("转移参保地行政区划代码", accountAc08.getAab299());
            map.put("转入业务起息年月", accountAc08.getAae729());
            map.put("缴费备用字段1", accountAc08.getAae819());
            map.put("缴费备用字段2", accountAc08.getAae820());
            map.put("双低系数（历史数据）", accountAc08.getAae796());
            map.put("固化社平工资（历史数据）", accountAc08.getAae797());
            map.put("基数比例（历史数据）", accountAc08.getAae798());
            map.put("固化指数", accountAc08.getAae799());
            map.put("账目类型", accountAc08.getAae800());
            map.put("征集流水号", accountAc08.getAaz625());
            map.put("费用标志", accountAc08.getAae792());
            map.put("实收年月", accountAc08.getAae150());
            map.put("实收分配流水号", accountAc08.getAae066());
            map.put("退费年月", accountAc08.getAae748());
            map.put("退还账户滚存利息标志", accountAc08.getAae822());
            map.put("退费批次号", accountAc08.getAaz631());
            map.put("退费流水号", accountAc08.getAaz650());
            map.put("备注", accountAc08.getAae013());
            map.put("经办人", accountAc08.getAae011());
            map.put("经办时间", accountAc08.getAae036());
            map.put("参保地行政区划代码", accountAc08.getAab359());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }


    //校验
    public void check(List<AccountAc08> businessMap) {


    }

    /**
     * 导入校验
     * @param row
     * @param i
     */
    @Override
    public String checkImport(Map row,Integer i) {


        StringBuilder stringBuilder = new StringBuilder();

        if(me.zhengjie.util.ValidateUtil.isEmpty(row.get("对应费款所属期"))){
            stringBuilder.append("第["+i+1+"]条数据:对应费款所属期不能为空;");
        }
        if(me.zhengjie.util.ValidateUtil.isEmpty(row.get("应缴类型"))){
            stringBuilder.append("第["+i+1+"]条数据:应缴类型不能为空;");
        }
        if(me.zhengjie.util.ValidateUtil.isEmpty(row.get("人员缴费基数"))){
            stringBuilder.append("第["+i+1+"]条数据:人员缴费基数不能为空;");
        }
        if(me.zhengjie.util.ValidateUtil.isEmpty(row.get("社平工资"))){
            stringBuilder.append("第["+i+1+"]条数据:社平工资不能为空;");
        }
        if(me.zhengjie.util.ValidateUtil.isEmpty(row.get("单位缴费比例"))){
            stringBuilder.append("第["+i+1+"]条数据:单位缴费比例不能为空;");
        }
        if(me.zhengjie.util.ValidateUtil.isEmpty(row.get("单位划入比例"))){
            stringBuilder.append("第["+i+1+"]条数据:单位划入比例不能为空;");
        }
        if(me.zhengjie.util.ValidateUtil.isEmpty(row.get("个人划入比例"))){
            stringBuilder.append("第["+i+1+"]条数据:个人划入比例不能为空;");
        }
        if(me.zhengjie.util.ValidateUtil.isEmpty(row.get("社平划入比例"))){
            stringBuilder.append("第["+i+1+"]条数据:社平划入比例不能为空;");
        }
        if(ValidateUtil.isEmpty(row.get("类型"))){
            stringBuilder.append("第["+i+1+"]条数据:类型不能为空;");
        }


       if(!isValidYyyyMm(row.get("对应费款所属期").toString())){
           stringBuilder.append("第["+i+1+"]条数据:对应费款所属期格式不正确,应该为yyyymm;");
       }

        List<String> aaa115s = Arrays.asList("正常应缴");

        if(!aaa115s.contains(row.get("应缴类型"))){
            stringBuilder.append("第["+i+1+"]条数据:应缴类型不在模版范围内;");

        }

        //判断是否为数字
        if(!isNumeric(row.get("人员缴费基数").toString())){
            stringBuilder.append("第["+i+1+"]条数据:人员缴费基数不是一个有效的数字;");
        }

        if(!isNumeric(row.get("社平工资").toString())){
            stringBuilder.append("第["+i+1+"]条数据:社平工资不是一个有效的数字;");
        }

        if(!isNumeric(row.get("单位缴费比例").toString())){
            stringBuilder.append("第["+i+1+"]条数据:单位缴费比例不是一个有效的数字;");
        }

        if(!isNumeric(row.get("单位划入比例").toString())){
            stringBuilder.append("第["+i+1+"]条数据:单位划入比例不是一个有效的数字;");
        }

        if(!isNumeric(row.get("个人划入比例").toString())){
            stringBuilder.append("第["+i+1+"]条数据:个人划入比例不是一个有效的数字;");
        }
        if(!isNumeric(row.get("社平划入比例").toString())){
            stringBuilder.append("第["+i+1+"]条数据:社平划入比例不是一个有效的数字;");
        }


        if(!isBetweenZeroAndOne(row.get("单位缴费比例").toString())){
            stringBuilder.append("第["+i+1+"]条数据:单位缴费比例必须大于0小于1;");
        }

        if(!isBetweenZeroAndOne(row.get("单位划入比例").toString())){
            stringBuilder.append("第["+i+1+"]条数据:单位划入比例比例必须大于0小于1;");
        }

//        if(!isBetweenZeroAndOne(row.get("个人划入比例").toString())){
//            stringBuilder.append("第["+i+1+"]条数据:个人划入比例必须大于0小于1;");
//        }

        if(!isBetweenZeroAndOne(row.get("社平划入比例").toString())){
            stringBuilder.append("第["+i+1+"]条数据:社平划入比例必须大于0小于1;");
        }

        return stringBuilder.toString();


    }


    /**
     * yyyymm格式校验
     * @param input
     * @return
     */
    public  boolean isValidYyyyMm(String input) {
        // 1. 检查是否为6位数字
        if (input == null || !input.matches("^\\d{6}$")) {
            return false;
        }

        // 2. 提取年份和月份
        int year = Integer.parseInt(input.substring(0, 4));
        int month = Integer.parseInt(input.substring(4, 6));

        // 3. 验证月份是否在1~12之间
        return month >= 1 && month <= 12;
    }

    /**
     * 判断必须是数字
     * @param str
     * @return
     */
    public  boolean isNumeric(String str) {
        if (str == null || str.isEmpty()) {
            return false;
        }
        // 匹配：可选负号 + 数字 + 可选（小数点 + 数字）
        return str.matches("-?\\d+(\\.\\d+)?");
    }

    /**
     * 判断比例大于0小于1
     * @param str
     * @return
     */
    public  boolean isBetweenZeroAndOne(String str) {
        if (str == null || str.isEmpty()) {
            return false;
        }
        try {
            double num = Double.parseDouble(str);
            return num >= 0 && num < 1;
        } catch (NumberFormatException e) {
            return false;
        }
    }



    public void saveLog(AccountAc08 resources,String aaa121,String date) throws IOException {

        //写日志
        Map map= new HashMap<>();

        //查询人员基本信息
        Map q = new HashMap();
        q.put("aac001",resources.getAac001());
        List<Map> list1 = accountCommonMapper.queryAc01Ac02(q);

        //查询单位基本信息
        QueryWrapper query2=new QueryWrapper();
        query2.eq("aab001",resources.getAab001());
        List<BasicAb01> list2 = basicAb01Mapper.selectList(query2);


        map.put("aaz002",sequenceService.nextSequence(new Sequence("aaz002")));
        map.put("aaa121",YHConstants.AAA121_020104);
        map.put("aab001",resources.getAab001());
        map.put("aab004",list2.get(0).getAab004());
        map.put("aac001",resources.getAac001());
        map.put("aac002",list1.get(0).get("aac002"));
        map.put("aac003",list1.get(0).get("aac003"));
        map.put("aae140",Constant.AAE140_120_JGSYDWGZRYJBYLBX);
        map.put("aae016",Constant.AAE016_1_SHTG);
        map.put("aae011",SecurityUtils.getCurrentUsername());
        map.put("aae036",date);
        map.put("aae014",SecurityUtils.getCurrentUsername());
        map.put("aae015",date);
        map.put("aab359","530111");

        //7、记录日志信息
        BusinessLog businessLog = BusinessLog.singleLog(map);
        businessLogService.save(businessLog);

    }

}
